<?php
class ControllerAccountLogin extends Controller {
	private $error = array();

	public function index()
	{

	    $this->load->model('account/customer');

		if (!$this->actionCheck(false,true)) {
            return;
	    }

	    if ($this->validate()) {
	        unset($this->session->data['guest']);

	        $token = null;
	        if(isset($this->request->post['device_token']) && utf8_strlen($this->request->post['device_token']) > 4)
	        {
	            $device_token = $this->request->post['device_token'];
	            $this->model_account_customer->editCustomerByKeyValue('device_token',$device_token);
	            $token = md5(uniqid(mt_rand(), true) . $device_token);
	            $this->model_account_customer->editCustomerByKeyValue('token',$token);
	        }

	        // Add to activity log
	        $this->load->model('account/activity');

	        $activity_data = array(
	            'customer_id' => $this->customer->getId(),
	            'name'        => $this->customer->getFirstName()
	        );

	        $this->model_account_activity->addActivity('login', $activity_data);

	        $customer_info = $this->customer->getCustomerInfo();

	        $this->load->model('tool/image');

	        if ($customer_info['image']) {
	            $customer_image = $this->model_tool_image->resize($customer_info['image'], $this->config->get('config_customer_image_width'), $this->config->get('config_customer_image_height'));
	        } else {
	            $customer_image = $this->model_tool_image->resize('no_head_image.jpg', $this->config->get('config_customer_image_width'), $this->config->get('config_customer_image_height'));
	        }

	        $customer_info['image'] = $customer_image;

	        $customer_info['token'] = $token;



	        if(isset($this->session->data['wx_unionid']))
	        {
	            $this->load->model('account/social');
	            $this->model_account_social->bind(array('uid'=>$this->session->data['wx_unionid'],'type'=>1));
	        }

	        $this->response->setDataResult(1,"",$customer_info);
	        return;

	    }
	    else
	   {
	       if (count($this->error)>0) {
	           $error_warning = array_shift($this->error);
	       } else {
	           $error_warning = '';
	       }
	       $this->response->setDataResult(0,$error_warning,null);
	       return;
	    }
	}

	protected function validate() {
		// Check how many login attempts have been made.
		$login_info = $this->model_account_customer->getLoginAttempts($this->request->post['phone']);

		if ($login_info && ($login_info['total'] > $this->config->get('config_login_attempts')) && strtotime('-1 hour') < strtotime($login_info['date_modified'])) {
			$this->error['warning'] = '您的帐号已超过登录尝试允许次数。请在1小时后再试。';
		}


		if (!$this->error) {
			if (!$this->customer->login($this->request->post['phone'], $this->request->post['password'])) {
				$this->error['warning'] = '账号或密码不匹配！';

				$this->model_account_customer->addLoginAttempt($this->request->post['phone']);
			} else {
				$this->model_account_customer->deleteLoginAttempts($this->request->post['phone']);
			}
		}

		return !$this->error;
	}


	public function simulationlogin(){
	    $this->customer->loginById("277");
	}


	public function loginout(){
	    if ($this->customer->isLogged()) {
	        $this->event->trigger('pre.customer.logout');

	        $this->customer->logout();
	        $this->cart->clear();

	        unset($this->session->data['wishlist']);
	        unset($this->session->data['shipping_address']);
	        unset($this->session->data['shipping_method']);
	        unset($this->session->data['shipping_methods']);
	        unset($this->session->data['payment_address']);
	        unset($this->session->data['payment_method']);
	        unset($this->session->data['payment_methods']);
	        unset($this->session->data['comment']);
	        unset($this->session->data['order_id']);
	        unset($this->session->data['coupon']);
	        unset($this->session->data['reward']);
	        unset($this->session->data['voucher']);
	        unset($this->session->data['vouchers']);
	        unset($this->session->data['wx_openid']);
	        $this->event->trigger('post.customer.logout');

	    }

	}
}